package com.liumingtai.learn.shiro.config.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.liumingtai.learn.shiro.module.test01.entity.BlogUser;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

@Component
public class OAuth2Realm extends AuthorizingRealm {
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {


        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String userName = (String) authenticationToken.getPrincipal();
        String password = new String((char[]) authenticationToken.getCredentials());
        BlogUser blogUser = new BlogUser().selectOne(new QueryWrapper<BlogUser>().lambda().eq(BlogUser::getUserName, userName).eq(BlogUser::getPassword, password));
        if (blogUser != null) {
            //可以登录
            SimpleAuthenticationInfo simpleAccount = new SimpleAuthenticationInfo(userName, password, getName());
            return simpleAccount;
        }
//        登录失败
//                .setUserName(userName).setPassword(password).
        return null;
    }
}
